// app.js
App({
    /*定时器变量*/
    notread_timer: false,

    onLaunch() {
        var that = this;
        that.notread_timer = setInterval(function () {
            var pages = getCurrentPages();
            var prevent_page_rouge = pages[pages.length - 1].route;
            //只有在可以看到红点的页面和不冲突页面，才会向服务器 发送请求获取数据，减少服务器负担
            if (prevent_page_rouge == "pages/message/message" || prevent_page_rouge == "pages/group/group" || prevent_page_rouge == "pages/userinfo/my") {
                that.scanInformation();
            }
        }, 10000)
    },
    
    globalData: {
        userInfo: null,
        userID: null,
        version: "1.0",
        Name: "新型程序备忘录",
        // domain:"http://127.0.0.1:8999/api",
        domain: "https://www.yosuzu.top/api",
        //未读信息
        notread_number: 0,
    },

    alert: function (params) {
        var title = params.hasOwnProperty('title') ? params['title'] : '温馨提示 ';
        var content = params.hasOwnProperty('content') ? params['content'] : '';
        var showCancel = params.hasOwnProperty('showCancel') ? params['showCancel'] : false;
        wx.showModal({
            title: title,
            content: content,
            showCancel: showCancel,
            success: function (res) {
                if (res.confirm) {//用户点击确定
                    if (params.hasOwnProperty('cb_confirm') && typeof (params.cb_confirm) == "function") {
                        params.cb_confirm();
                    }
                } else {
                    if (params.hasOwnProperty('cb_cancel') && typeof (params.cb_cancel) == "function") {
                        params.cb_cancel();
                    }
                }
            }
        })
    },

    buildUrl: function (path, params) {
        var url = this.globalData.domain + path;
        var _paramUrl = "";
        if (params) {
            _paramUrl = Object.keys(params).map(function (k) {
                return [encodeURIComponent(k), encodeURIComponent(params[k])].join("=");
            }).join("&");
            _paramUrl = "?" + _paramUrl;
        }
        return url + _paramUrl;
    },

    //构造请求头
    getRequestHeader: function () {
        return {
            'content-type': 'application/x-www-form-urlencoded',
            'Authorization': this.getCache("token"),
        }
    },

    //接收服务器回传数据并写入缓存
    getGroupData: function () {
        var that = this;
        wx.request({
            url: this.buildUrl("/group/index"),
            header: this.getRequestHeader(),
            data: {
                user_id: this.globalData.userID,
            },
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    this.alert({
                        "content": resp.msg
                    });
                    return;
                }
                that.setCache('manager_group_data_list', resp.data.manager_group_data_list);
                that.setCache('member_group_data_list', resp.data.member_group_data_list);
            }
        })
    },

    //将缓存的群组信息写入data
    setGroupData: function (that, manager_key = "manager_list", member_key = "member_list") {
        if (manager_key) {
            var manager_list = this.getCache('manager_group_data_list');
            that.setData({
                [manager_key]: manager_list,
            })
        }
        if (member_key) {
            var member_list = this.getCache('member_group_data_list');
            that.setData({
                [member_key]: member_list,
            })
        }
    },

    getCache: function (key) {
        var value = undefined;
        try {
            value = wx.getStorageSync(key);
        } catch (e) {
        }
        return value;
    },

    setCache: function (key, value) {
        wx.setStorage({
            key: key,
            data: value
        });
    },

    scanInformation: function () {
        var that = this;
        /*向服务器发送请求获取信息 */

        wx.request({
            url: that.buildUrl("/message/information-count"),
            header: that.getRequestHeader(),
            data: {
                'user_id': that.globalData.userID
            },
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    that.alert({
                        "content": resp.msg
                    });
                    return;
                }
                that.globalData.notread_number = resp.data.count;
                if (that.globalData.notread_number) {
                    wx.setTabBarBadge({
                        index: 2,
                        text: "" + that.globalData.notread_number + "",
                    })
                }
                else {
                    wx.removeTabBarBadge({
                        index: 2,
                    })
                }
            }
        })
    },
})

